package com.ebay.services;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;

import com.ebay.util.DBconn;

public class Services {


	public static LinkedHashMap<String,String> getSubcategoryList(int category,int subcategory){
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		String subCatName = null;
		String subCatLink = null;
		LinkedHashMap<String,String> subCatList = new LinkedHashMap<String,String>();
		String query = "select * from subcategory where category_id = "+category;
		con = DBconn.getConnection();
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(query);
			while(rs.next()){
				subCatName = rs.getString("subcategory_name");
				if(subcategory!=rs.getInt("subcategory_id"))
					subCatLink = "?category_id="+category+"&subcategory_id="+rs.getInt("subcategory_id");
				else
					subCatLink = "";
				subCatList.put(subCatLink,subCatName);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return subCatList;
	}

	public static String getEndTimeText(Date endTime){
		Calendar d1 = Calendar.getInstance();
		Calendar d2 = Calendar.getInstance();
		long diffSeconds, diffMins, diffHours, diffDays;
		String timeLeftString="";
		//d1.setTime(StartTime);
		d2.setTime(endTime);
		diffSeconds = (d2.getTimeInMillis()-d1.getTimeInMillis())/1000;
		diffDays = diffSeconds/(60*60*24);
		//if(diffDays>20){
		//timeLeftString = new SimpleDateFormat("d MMMM yyyy, KK:mm:ss aa").format(EndTime);
		//}else{
		diffSeconds -= diffDays * (60*60*24);
		diffHours = diffSeconds/(60*60);
		diffSeconds -= diffHours * (60*60);
		diffMins = diffSeconds/60;
		diffSeconds -= diffMins * 60;
		if(diffDays != 0){
			timeLeftString = diffDays + "d ";
		}
		if(diffHours != 0){
			timeLeftString += diffHours +"h ";
		}
		if(diffMins != 0){
			timeLeftString += diffMins +"m ";
		}
		if(diffSeconds != 0){
			timeLeftString += diffSeconds +"s ";
		}
		return timeLeftString;
	}

}
